package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.jingdianjichi.auth.application.converter.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthUserDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/*
* AuthUserController
* */
@RestController
@RequestMapping("/user")
@Slf4j
public class AuthUserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /*
    * 注册用户
    * */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.register.authUserDTO:{}",authUserDTO);
            }

            Objects.requireNonNull(authUserDTO.getUserName(),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.Instance.DTOConverterBO(authUserDTO);
            Boolean result = authUserDomainService.register(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AuthUserController.register.authUserDTO:{}",e.getMessage(),e);
            return Result.fail("注册用户失败");
        }
    }

    /*
    * 更新用户信息
    * */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
               log.info("AuthUserController.update.authUserDTO:{}",authUserDTO);
            }
            Objects.requireNonNull(authUserDTO.getId(),"用户的Id不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.Instance.DTOConverterBO(authUserDTO);
            Boolean result = authUserDomainService.update(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AuthUserController.update.authUserDTO:{}",e.getMessage(),e);
            return Result.fail("更新用户信息失败");
        }
    }

    /*
    * 获取用户信息
    * */
    @PostMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.getUserInfo.authUserDTO:{}",authUserDTO);
            }

            Objects.requireNonNull(authUserDTO.getUserName(),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.Instance.DTOConverterBO(authUserDTO);
            return Result.ok(AuthUserDTOConverter.
                    Instance.BOConverterDTO(authUserDomainService.getUserInfo(authUserBO)));
        } catch (Exception e) {
            log.error("AuthUserController.getUserInfo.authUserDTO:{}",e.getMessage(),e);
            return Result.fail("获取用户信息失败");
        }
    }

    /*
    * 退出登录
    * */
    @PostMapping("/logOut")
    public Result logOut(@RequestParam String userName){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.logOut.userName:{}",userName);
            }

            Objects.requireNonNull(userName,"用户名不能为空");

            StpUtil.logout(userName);
            return Result.ok();
        } catch (Exception e) {
            log.error("AuthUserController.logOut.userName:{}",e.getMessage(),e);
            return Result.fail("退出登陆失败");
        }
    }

    /*
    * 删除用户信息
    * */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.delete.authUserDTO:{}",authUserDTO);
            }
            Objects.requireNonNull(authUserDTO.getId(),"用户的Id不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.Instance.DTOConverterBO(authUserDTO);
            Boolean result = authUserDomainService.delete(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AuthUserController.delete.authUserDTO:{}",e.getMessage(),e);
            return Result.fail("删除用户信息");
        }
    }

    /*
    * 修改用户状态
    * */
    @PostMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthUserController.changeStatus.authUserDTO:{}",authUserDTO);
            }
            Objects.requireNonNull(authUserDTO.getId(),"用户的Id不能为空");
            Objects.requireNonNull(authUserDTO.getStatus(),"用户的状态不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.Instance.DTOConverterBO(authUserDTO);
            Boolean update = authUserDomainService.update(authUserBO);
            return Result.ok(update);
        } catch (Exception e) {
            log.error("AuthUserController.changeStatus.authUserDTO:{}",e.getMessage(),e);
            return Result.fail("修改用户状态失败");
        }
    }

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam String validCode) {
        try {
            Objects.requireNonNull(validCode,"验证码不能为空");
            return Result.ok(authUserDomainService.doLogin(validCode));
        } catch (Exception e) {
            log.error("AuthUserController.doLogin.validCode:{}",e.getMessage(),e);
            return Result.fail("用户登陆失败");
        }
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
//        if("zhang".equals(username) && "123456".equals(password)) {
//            StpUtil.login("鸡翅");
//            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
//            //第三步，返还给前端
//            return SaResult.data(tokenInfo);
//        }
//        return SaResult.error("登录失败");
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        System.out.println(StpUtil.isLogin());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        System.out.println(tokenInfo);
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
